#include "duck.h"

//macros
#define I_TOOLBAR					0
#define IDM_SPAWN					0
#define IDM_TOQUEST					1

//variables
GLOBAL HFONT hFontRegular;
GLOBAL HFONT hFontBold;
HWND hToquestButton;
INT timeToNextToquestAvailable = 0;

//declearations
VOID __stdcall Timerproc(HWND hwnd, UINT, UINT_PTR ptr, DWORD);
LRESULT __stdcall TipWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);


VOID __stdcall CreateTipWindow()
{
	gIsTipShown = TRUE;

	WNDCLASS wndc = {};
	wndc.lpfnWndProc = TipWindowProc;
	wndc.hInstance = ghInstance;
	wndc.lpszClassName = DUCKPROC_CLASS_TIP_NAME;
	wndc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
	auto dd = RegisterClass(&wndc);

	RECT rect;
	SystemParametersInfo(SPI_GETWORKAREA, NULL, &rect, NULL);

	const HWND hTipWindow = CreateWindowEx(
		WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
		DUCKPROC_CLASS_TIP_NAME,
		DUCKPROC_WINDOW_TIP_NAME,
		WS_POPUP | WS_BORDER,
		rect.right - 360 * dpisc,
		rect.bottom - 120 * dpisc,
		360 * dpisc,
		120 * dpisc,
		nullptr,
		nullptr,
		ghInstance,
		nullptr
	);

	ShowWindow(hTipWindow, SW_SHOWDEFAULT);
}


LRESULT __stdcall TipWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CREATE:
		{
			//load resourced fonts into global variables

			const HRSRC hFontRegularRes = FindResource(ghInstance, MAKEINTRESOURCE(2), TEXT("GADOWS"));
			const HGLOBAL hFontRegularMem = LoadResource(ghInstance, hFontRegularRes);
			const LPVOID hFontRegularData = LockResource(hFontRegularMem);
			DWORD nFontRegular = 0, lenFontRegular = SizeofResource(ghInstance, hFontRegularRes);
			AddFontMemResourceEx(hFontRegularData, lenFontRegular, nullptr, &nFontRegular);

			const HRSRC hFontBoldRes = FindResource(ghInstance, MAKEINTRESOURCE(3), TEXT("GADOWS"));
			const HGLOBAL hFontBoldMem = LoadResource(ghInstance, hFontBoldRes);
			const LPVOID hFontBoldData = LockResource(hFontBoldMem);
			DWORD nFontBold = 0, lenFontBold = SizeofResource(ghInstance, hFontBoldRes);
			AddFontMemResourceEx(hFontBoldData, lenFontBold, nullptr, &nFontBold);


			//get fonts

			const HDC hdc = GetDC(hwnd);
			HFONT hFontRegular = GetFont(hdc, TEXT("Overpass Mono"), dpisc*13, FW_REGULAR);
			HFONT hFontBold = GetFont(hdc, TEXT("Overpass Mono"), dpisc*13, FW_BOLD);
			ReleaseDC(hwnd, hdc);


			//create controls while setting fonts

			SendMessage(CreateWindow(
				TEXT("STATIC"),
				TEXT("You cannot get rid of the ducks until you pass the duck quest"),
				WS_VISIBLE | WS_CHILD | SS_LEFT,
				dpisc * 10,
				dpisc * 10,
				dpisc * 340,
				dpisc * 60,
				hwnd,
				NULL,
				ghInstance,
				NULL), WM_SETFONT, (WPARAM)hFontRegular, static_cast<LPARAM>(MAKELONG(TRUE, 0)));


			//create the toolbar and set its font

			const HWND hToolbar = CreateWindow(TOOLBARCLASSNAME, NULL,
				WS_CHILD | TBSTYLE_FLAT|TBSTYLE_AUTOSIZE | TBSTYLE_LIST|CCS_BOTTOM, 0, 0, 0, 0,
				hwnd, NULL, ghInstance, NULL);
			SendMessage(hToolbar, WM_SETFONT, (WPARAM)hFontBold, static_cast<LPARAM>(MAKELONG(TRUE, 0)));


			//create the corresponding imagelist and attach it to the toolbar

			HIMAGELIST hImagelist = ImageList_Create(32 * dpisc, 32 * dpisc,ILC_COLOR32, 2, 0);

			{
				const HBITMAP hbSpawn = static_cast<HBITMAP>(LoadImage(ghInstance,
					MAKEINTRESOURCE(ID_BMP_SPAWN),IMAGE_BITMAP, 32 * dpisc, 32 * dpisc, LR_CREATEDIBSECTION));
				const HBITMAP hbToquest = static_cast<HBITMAP>(LoadImage(ghInstance,
					MAKEINTRESOURCE(ID_BMP_TOQUEST),IMAGE_BITMAP, 32 * dpisc, 32 * dpisc, LR_CREATEDIBSECTION));

				ImageList_Add(hImagelist, hbSpawn, nullptr);
				ImageList_Add(hImagelist, hbToquest, nullptr);
			}

			SendMessage(hToolbar, TB_SETIMAGELIST, I_TOOLBAR, (LPARAM)hImagelist);


			//create context buttons to the toolbar

			TBBUTTON tbButtons[] =
			{
				{I_IMAGENONE, NULL, TBSTYLE_TRANSPARENT | BTNS_SEP, BTNS_AUTOSIZE, {}, 0, (INT_PTR)L"         "},
				{0, IDM_SPAWN, TBSTATE_ENABLED, BTNS_AUTOSIZE, {}, 0, (INT_PTR)L"spawn"},
				{I_IMAGENONE, NULL, TBSTYLE_TRANSPARENT | BTNS_SEP, BTNS_AUTOSIZE, {}, 0, (INT_PTR)L" "},
				{1, IDM_TOQUEST, TBSTATE_ENABLED, BTNS_AUTOSIZE, {}, 0, (INT_PTR)L"to quest"},
			};
			SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, static_cast<WPARAM>(sizeof(TBBUTTON)), 0);
			SendMessage(hToolbar, TB_ADDBUTTONS, sizeof tbButtons / sizeof(TBBUTTON), (LPARAM)&tbButtons);
			SendMessage(hToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(0, 40));
			SendMessage(hToolbar, TB_AUTOSIZE, 0, 0);
			ShowWindow(hToolbar, TRUE);

			return 0;
		}
	case WM_CTLCOLORSTATIC:
		{
			SetBkColor((HDC)wParam, RGB(255, 255, 255));
			return 0;
		}
	case WM_SYSCOMMAND:
		{
			if ((wParam & 0xfff0) == SC_MOVE) return 0;
			break;
		}
	case WM_COMMAND:
		{
			if (LOWORD(wParam) == IDM_SPAWN && !timeToNextToquestAvailable)
			{
				SpreadDucksOnce(nullptr);
			}
			break;
		}
		/*case WM_QUESTBACK:
			{
				timeToNextToquestAvailable = QUEST_INTERVAL_SEC + 1;
				SetTimer(hwnd, NULL, 1000, Timerproc);
				Timerproc(nullptr, 0, 0, 0);
				return 0;
			}*/
	default:
		break;
	}

	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


VOID __stdcall Timerproc(HWND hwnd, UINT, UINT_PTR ptr, DWORD)
{
	timeToNextToquestAvailable--;
	TCHAR str[13];
	wsprintf(str, TEXT("%d'%d\""), timeToNextToquestAvailable / 60, timeToNextToquestAvailable % 60);
	SetWindowText(hToquestButton, str);
	if (timeToNextToquestAvailable <= 0)
	{
		timeToNextToquestAvailable = 0;
		SetWindowText(hToquestButton, TEXT("Duck Quest"));
		EnableWindow(hToquestButton, TRUE);
		KillTimer(hwnd, ptr);
	}
}
